﻿jQuery(document).ready(function () {
    $('#navi .root > li > div.a').hover(function () {
        $(this).next().stop(true, true).slideDown(300);
    }, function () {
        $(this).removeClass('active');
    });
    $('#navi .root > li').hover(function () {
        $(this).find('div:first').next().stop(true, true).slideDown(300);
        $(this).find('div:first').addClass('active');
    }, function () {
        $(this).find('div:first').next().stop(true, true).slideUp(300);
        $(this).find('div:first').removeClass('active');
    });
    jQuery('#mycarousel').jcarousel({
        auto: 3,
        scroll: 1,
        wrap: 'last',
        initCallback: mycarousel_initCallback
    });

    $('.box-menu div.content-box-3').hide();
    $('.box-menu >.tittle-box-full-2 ').click(function () {
        $(this).next().addClass('ulActive');
        //$('.box-menu div.content-box-3:visible:not(.ulActive)').hide('normal');
        $(this).next().stop(true, true).removeClass('ulActive').toggle('normal');
        return false;
    });
    $('.aq-items div.answer').hide();
    $('.aq-items > .question ').click(function () {
        $(this).next().addClass('ulActive');
        //$('.box-menu div.content-box-3:visible:not(.ulActive)').hide('normal');
        $(this).next().stop(true, true).removeClass('ulActive').toggle('normal');
        return false;
    });
    setActiveNav();
    try {
        $('#partner-slide a').lightBox();
    }
    catch (e) { }
    setupJW($("#hfLinkVideos").val());
    
    $(".flag").live('click', function() {
        var url = location.url;
        console.log(url);
    });
});

function mycarousel_initCallback(carousel) {
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function () {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function () {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function () {
        carousel.stopAuto();
    }, function () {
        carousel.startAuto();
    });
};

//marqueeInit({
//    uniqueid: 'partner-slide',
//    style: {
//        'padding': '0',
//        'width': '100%',
//        'height': '100%'
//    },
//    inc: 10, //speed - pixel increment for each iteration of this marquee's movement
//    mouse: 'cursor driven', //mouseover behavior ('pause' 'cursor driven' or false)
//    moveatleast: 1,
//    neutral: 150,
//    noAddedSpace: true,
//    savedirection: true
//});

function setupJW(url) {
    jwplayer('mediaplayer').setup({
        'flashplayer': '/App_Plugins/mediaplayer/player.swf',
        'id': 'playerID',
        'width': '100%',
        'height': '210px',
        'autostart': 'false',
        'file': getList(url, "v"),
        'provider': 'youtube',
        'plugins': {
            'hd-2': {}
        }
    });
};

var getList = function (url, gkey) {
    var returned = null;
    if (url.indexOf("?") != -1) {
        var list = url.split("?")[1].split("&"), gets = [];
        for (var ind in list) {
            var kv = list[ind].split("=");
            if (kv.length > 0)
                gets[kv[0]] = kv[1];
        }
        returned = gets;
        if (typeof gkey != "undefined")
            if (typeof gets[gkey] != "undefined")
                returned = gets[gkey];
    }
    return returned;
};

function CheckValidBoxSearch() {
    var keyword = $('#txtKeyword').val();
    var defaultText = 'Nhập từ khóa tìm kiếm';
    if (keyword == defaultText || keyword == '') {
        alert("Hãy nhập từ khóa tìm kiếm !");
        return false;
    } else {
        return true;
    }
}

function LoadAdvertisement(page, position, key) {
    $.ajax({
        type: "POST",
        url: "/WebServices/AdsService.asmx/GetAdvertisement",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'pageAlias':'" + page + "','positionAlias':'" + position + "','key':'" + key + "'}",
        beforeSend: function () {
        },
        async: true,
        success: function (m) {
            var result = m.d;
            if (result.length > 0) {
                $("#" + page + "-" + position).html(m.d);
            }
        }
    });
}

var UrlEncode = {
 
    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },
 
    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },
 
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";
 
        for (var n = 0; n < string.length; n++) {
 
            var c = string.charCodeAt(n);
 
            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }
 
        }
 
        return utftext;
    },
 
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
 
        while ( i < utftext.length ) {
 
            c = utftext.charCodeAt(i);
 
            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
 
        }
        return string;
    }
};